package oauth.controller;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import common.Crypto;
import oauth.repository.mapper.SysUsersMapper;
import oauth.repository.mapper.SysUsersRoleMapper;
import oauth.repository.mapperadd.SpringSecurityMapper;
import oauth.repository.mapperadd.SysUsersMapperAdd;
import oauth.repository.model.SysUsers;
import oauth.repository.model.SysUsersRole;
import oauth.service.security.SpringSecurityLogin;

/**
 * @author Pan JinWen
 * @time 2016年5月20日下午4:50:51
 */
@Controller
public class Users
{
	@Autowired
	private SysUsersMapper sysUsersMapper;
	@Autowired
	private SysUsersMapperAdd sysUsersMapperAdd;
	@Autowired
	private SysUsersRoleMapper sysUsersRoleMapper;
	@Autowired
	private SpringSecurityLogin springSecurityLogin;
	@Autowired
	private SpringSecurityMapper springSecurityMapper;

	// view选择前置
	public void init(Map<String, Object> map)
	{
		Map<String, String> status = new HashMap<String, String>();
		status.put("1", "normal");
		status.put("0", "locked");
		map.put("status", status);

		Map<String, String> desc = new HashMap<String, String>();
		desc.put("管理员", "管理员");
		desc.put("操作员", "操作员");
		desc.put("普通用户", "普通用户");
		map.put("desc", desc);
	}

	// 用户信息前置
	@RequestMapping("/newUser")
	public String newUser(Map<String, Object> map)
	{
		this.init(map);
		SysUsers sysUsers = new SysUsers();
		sysUsers.setStatus((short) 0);
		sysUsers.setDescn("普通用户");
		map.put("users", sysUsers);
		return "sign-up";
	}

	// 用户列表
	// @Secured({ "ROLE_ADMIN", "IP_LOCAL_HOST" })
	@RequestMapping("/showUsers")
	public String list(Map<String, Object> map)
	{
		map.put("users", sysUsersMapperAdd.selectAllUsers());
		return "showUsers";
	}

	// 查询用户
	@RequestMapping(value = "/users/{id}", method = RequestMethod.GET)
	public String updateuser(@PathVariable BigDecimal id, Map<String, Object> map)
	{
		this.init(map);
		SysUsers selectByPrimaryKey = sysUsersMapper.selectByPrimaryKey(id);
		map.put("users", selectByPrimaryKey);
		return "sign-up";
	}

	// 新增用户
	@RequestMapping(value = "/users", method = RequestMethod.POST)
	public String saveUser(SysUsers users, HttpServletRequest request)
	{
		users.setPassword(Crypto.MD5(users.getPassword(), "erhuoxiaoliang", true));
		sysUsersMapper.insertSelective(users);
		BigDecimal selectUserIdByUsername = sysUsersMapperAdd.selectUserIdByUsername(users.getUsername());
		// 新增默认用户角色
		SysUsersRole sysUsersRole = new SysUsersRole();
		sysUsersRole.setUsersId(selectUserIdByUsername);
		sysUsersRole.setRoleId(new BigDecimal(1));
		sysUsersRoleMapper.insert(sysUsersRole);
		springSecurityLogin.login(users.getUsername(), request);
		return "redirect:/showUsers";
	}

	// 更新用户信息
	@RequestMapping(value = "/users", method = RequestMethod.PUT)
	public String updateUser(SysUsers users)
	{
		if (users.getPassword().trim().equals("") || users.getPassword() == null)
		{
			users.setPassword(null);
		}
		else
		{
			users.setPassword(Crypto.MD5(users.getPassword(), "erhuoxiaoliang", true));
		}
		sysUsersMapper.updateByPrimaryKeySelective(users);
		return "redirect:/showUsers";
	}
	// 更新角色信息

	// 删除用户
	@RequestMapping(value = "/users/{id}", method = RequestMethod.DELETE)
	public String deleteuser(@PathVariable BigDecimal id)
	{
		springSecurityMapper.deleteByUserId(id);
		sysUsersMapperAdd.deleteUsersRoleByUserId(id);
		sysUsersMapper.deleteByPrimaryKey(id);
		return "redirect:/showUsers";
	}
}
